﻿using System;
using System.ComponentModel.DataAnnotations;
using XStudio.XCRF.Utilities.Regex;
using XStudio.XCRF.Data.Business.Archieve;
using XStudio.XCRF.Data.Business.Validate;
using XStudio.XCRF.Utilities.MD5Encryption;
using XStudio.XCRF.Utilities.Base64Encryption;
using XStudio.XCRF.Data.Enums;

namespace XStudio.XCRF.Data.Entities
{
    /// <summary>
    /// 系统用户
    /// </summary>
    [MemberValidate()]
    public partial class Member : BaseEntity<Member>, IArchieve
    {
        #region 属性
        /// <summary>
        /// 登陆名
        /// </summary>
        [ArchieveField]
        [Required]
        [Column(TypeName="varchar")]
        [MaxLength(20)]
        public string LoginName { get; set; }
        /// <summary>
        /// 密码MD5密文
        /// </summary>
        [ArchieveField]
        [Required]
        [RegularExpression(RegexUtil.REGEX_MD5_32)]
        [Column(TypeName = "char")]
        [StringLength(32)]
        [DataType(DataType.Password)]
        public string PasswordMD5 { get; set; }
        /// <summary>
        /// 真实姓名
        /// </summary>
        [ArchieveField]
        [Column(TypeName = "varchar")]
        public string RealName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        [ArchieveField]
        [Required]
        [EnumDataType(typeof(GenderTypes))]
        public byte Gender { get; set; }
        /// <summary>
        /// 联系电话
        /// </summary>
        [ArchieveField]
        [Column(TypeName = "varchar")]
        [DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }
        /// <summary>
        /// 电子邮箱
        /// </summary>
        [ArchieveField]
        [Column(TypeName = "varchar")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }
        /// <summary>
        /// 用户类型
        /// </summary>
        [ArchieveField]
        [Required]
        [EnumDataType(typeof(MemberTypes))]
        public byte Type { get; set; }
        /// <summary>
        /// 个人简介
        /// </summary>
        [ArchieveField]
        [Column(TypeName = "varchar")]
        [DataType(DataType.MultilineText)]
        public string Profile { get; set; }
        /// <summary>
        /// 所属医院ID
        /// </summary>
        [ArchieveField]
        [ForeignKey("Hospital")]
        public Guid HospitalID { get; set; }
        /// <summary>
        /// 所属医院
        /// </summary>
        [InverseProperty("Members")]
        public virtual Hospital Hospital { get; set; }
        #endregion 属性

        #region 扩展属性
        /// <summary>
        /// 设置成员密码
        /// </summary>
        [NotMapped]
        public string Password
        {
            get { return null; }
            set { PasswordMD5 = value.MD5Encode32(); }
        }
        /// <summary>
        /// 设置成员密码Base64密文
        /// <para>为了绑定页面必须实现get方法，实际get方法无意义，返回null占位</para>
        /// </summary>
        [NotMapped]
        public string Passcode
        {
            get { return null; }
            set { Password = value.Base64Decode(); }
        }
        /// <summary>
        /// 获取是否是第一次登录
        /// </summary>
        [NotMapped]
        public bool IsFirst { get { return CreateTime == LastModifyTime; } }
        #endregion 扩展属性

        #region 业务方法
        /// <summary>
        /// 验证用户密码是否正确
        /// </summary>
        /// <param name="password">待验证的密码</param>
        /// <returns></returns>
        public bool Validate(string password)
        {
            string md5 = password.MD5Encode32();
            return PasswordMD5 == md5;
        }

        /// <summary>
        /// 验证原密码并修改为新密码
        /// </summary>
        /// <param name="oldPassword">原密码</param>
        /// <param name="newPassword">新密码</param>
        public void ChangePassword(string oldPassword, string newPassword)
        {
            if (!Validate(oldPassword))
                throw new Exception("密码错误");
            Password = newPassword;
        }

        /// <summary>
        /// 直接修改新密码
        /// </summary>
        /// <param name="newPassword">新密码</param>
        public void ChangePassword(string newPassword)
        {
            Password = newPassword;
        }

        #endregion 业务方法
    }
}
